package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.just.agentweb.AgentWeb;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.GoodInfoDetail;
import org.gangcai.mac.shop.bean.HomeDataBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.easypop.CmmtPopup;
import org.gangcai.mac.shop.holders.LocalImageHolderView;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.manager.RetrofitManageres;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.util.ToastUitl;
import org.gangcai.mac.shop.utils.UserInfoUtils;
import org.json.JSONArray;
import org.json.JSONException;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.leefeng.promptlibrary.PromptDialog;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;
import static org.gangcai.mac.shop.constants.Constant.CODE_SUCC;

public class TTShopDetailActivity extends MultiStatusActivity {

    public PromptDialog prompDialog;
    private String tid;
    private String term_id;
    private String postID;
    private GoodInfoDetail goodInfo;
    private AgentWeb mAgentWeb;
    private String post_title;
    private String post_authorname;
    private String post_area;
    private String post_price;
    private String post_date;
    private String photos_urls;
    private String photos_content;
    @BindView(R.id.smetaImg_layout)
    LinearLayout smetaImg_layout;
    @BindView(R.id.smetaImg)
    ImageView smetaImg;
    @BindView(R.id.smetaImg1)
    ImageView smetaImg1;
    @BindView(R.id.smetaImg2)
    ImageView smetaImg2;
    @BindView(R.id.smetaImg3)
    ImageView smetaImg3;
    @BindView(R.id.smetaImg4)
    ImageView smetaImg4;
    @BindView(R.id.smetaImg5)
    ImageView smetaImg5;
    @BindView(R.id.smetaImg6)
    ImageView smetaImg6;
    @BindView(R.id.smetaImg7)
    ImageView smetaImg7;
    @BindView(R.id.smetaImg8)
    ImageView smetaImg8;
    @BindView(R.id.showTitle)
    TextView showTitle;
    @BindView(R.id.showTitle1)
    TextView showTitle1;
    @BindView(R.id.showPrice)
    TextView showPrice;
    @BindView(R.id.showDate)
    TextView showDate;
    @BindView(R.id.line_text1)
    TextView line_text1;
    @BindView(R.id.line_text2)
    TextView line_text2;
    @BindView(R.id.line_text3)
    TextView line_text3;
    @BindView(R.id.showContent)
    TextView showContent;
    @BindView(R.id.showFavouriteBtn)
    Button showFavouriteBtn;
    @BindView(R.id.showMomenyBtn)
    Button showMomenyBtn;
    private CmmtPopup mCmmtPopup;
    private String uidString;
    private String nickName;
    private String mobileString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        ButterKnife.bind( this );
        uidString = UserInfoUtils.getAppUserId( this );
        nickName = UserInfoUtils.getNickName( this );
        tid = getIntent().getStringExtra( "tid" );
        term_id = getIntent().getStringExtra( "term_id" );
        postID = getIntent().getStringExtra( "post_id" );
        post_title = getIntent().getStringExtra( "post_title" );
        post_authorname = getIntent().getStringExtra( "post_authorname" );
        post_area = getIntent().getStringExtra( "post_area" );
        post_price = getIntent().getStringExtra( "post_price" );
        if(post_price.equals( "0.00" )){
            post_price="价格可议";
        }
        post_date = getIntent().getStringExtra( "post_date" );
        photos_urls = getIntent().getStringExtra( "photos_urls" );
        mobileString = getIntent().getStringExtra( "mobile" );
        photos_content = getIntent().getStringExtra( "photos_content" );
        initTitleBarView( titlebar, "商品详情" );
        showTitle1.setText( post_title );
        showPrice.setText( post_price );
        showDate.setText( post_date );
        line_text3.setText( post_area );
        showContent.setText( photos_content );
        if (term_id.equals( "3" )) {
            smetaImg_layout.setVisibility( View.VISIBLE );
            if (!TextUtils.isEmpty( photos_urls ) && photos_urls.length() > 32) {
                try {
                    JSONArray imageArray = new JSONArray( photos_urls );
                    if (imageArray != null && imageArray.length() > 0) {
                        for (int i = 0; i < imageArray.length(); i++) {
                            String str = imageArray.getString( i );
                            if (!str.startsWith( "http://" )) {
                                str = Constant.THINKCMF_PATH + str;
                            }
                            switch (i) {
                                case 0:
                                    Glide.with( this ).load( str ).into( smetaImg );
                                    break;
                                case 1:
                                    Glide.with( this ).load( str ).into( smetaImg1 );
                                    break;
                                case 2:
                                    Glide.with( this ).load( str ).into( smetaImg2 );
                                    break;
                                case 3:
                                    Glide.with( this ).load( str ).into( smetaImg3 );
                                    break;
                                case 4:
                                    Glide.with( this ).load( str ).into( smetaImg4 );
                                    break;
                                case 5:
                                    Glide.with( this ).load( str ).into( smetaImg5 );
                                    break;
                                case 6:
                                    Glide.with( this ).load( str ).into( smetaImg6 );
                                    break;
                                case 7:
                                    Glide.with( this ).load( str ).into( smetaImg7 );
                                    break;
                                case 8:
                                    Glide.with( this ).load( str ).into( smetaImg8 );
                                    break;
                                default:
                                    Glide.with( this ).load( str ).into( smetaImg );
                                    break;
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }else{
            showTitle.setText( "【求购】" );

        }

        initCmmtPop();

    }

    private void initCmmtPop() {
        mCmmtPopup = CmmtPopup.create( this )
                .setOnCancelClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mCmmtPopup.isShowing()) {
                            //无法隐藏输入法。只有toggle方法起作用...
                            KeyboardUtils.hideSoftInput( TTShopDetailActivity.this );
                            mCmmtPopup.hideSoftInput()
                                    .dismiss();
                        }
                    }
                } )
                .setOnOkClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mCmmtPopup.isShowing()) {
                            String comment = mCmmtPopup.mEditText.getText().toString();
                            if (comment != null && comment.length() > 4) {
//无法隐藏输入法。只有toggle方法起作用...
                                KeyboardUtils.hideSoftInput( TTShopDetailActivity.this );
                                mCmmtPopup
                                        .dismiss();
                                RetrofitManageres.getInstance()
                                        .create( AppAPI.class )
                                        .PostArticleComment( uidString, postID, nickName, comment )
                                        .observeOn( AndroidSchedulers.mainThread() )
                                        .subscribeOn( Schedulers.io() )
                                        .subscribe( new CommonObserver<CommonResonseBean>() {
                                            @Override
                                            public void onSuccess(CommonResonseBean value) {
                                                if (value.getCode() == CODE_SUCC) {

                                                }
                                                ToastUitl.showShort( "非常感谢您的报价,商家会在收到信息后及时与您联系,请耐心等待" );
                                            }

                                            @Override
                                            public void onError(Throwable e) {
                                                super.onError( e );
                                                ToastUitl.showShort( "非常感谢您的报价,商家会在收到信息后及时与您联系,请耐心等待" );
                                            }
                                        } );
                            } else {
                                ToastUitl.showShort( "请您输入有效报价" );
                            }
                        }
                    }
                } )
                .apply();

    }

    private void showCmmtPop(View view) {
        mCmmtPopup.showSoftInput()
                .showAtLocation( view, Gravity.BOTTOM, 0, 0 );
    }

    private void loadData() {
        prompDialog.showLoading( "请等待" );
        RetrofitManageres.getInstance().create( AppAPI.class ).HomeData( "Portal", "list", "indexJson", "3" )
                .observeOn( AndroidSchedulers.mainThread() )
                .compose( this.<HomeDataBean>bindToLifecycle() )
                .subscribe( new CommonObserver<HomeDataBean>( prompDialog ) {
                    @Override
                    public void onSuccess(HomeDataBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            prompDialog.showSuccess( "提交成功" );
                        }
                    }
                } );

    }

    @Override
    public int getContentView() {
        return R.layout.activity_shop_detail_view;
    }

    @OnClick({R.id.showFavouriteBtn, R.id.showMomenyBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.showFavouriteBtn:
                if(uidString.equals( "0" )){
                    Toast.makeText( TTShopDetailActivity.this, getString( R.string.toast_person_unlogin ), Toast.LENGTH_SHORT ).show();
                    ActivityUtils.startActivity( LoginActivity.class);
                }else{
                    RetrofitManageres.getInstance().create(AppAPI.class).DoFavorite(uidString,post_title,"posts",postID)
                            .observeOn( AndroidSchedulers.mainThread())
                            .subscribeOn( Schedulers.io())
                            .subscribe(new CommonObserver<CommonResonseBean>() {
                                @Override
                                public void onSuccess(CommonResonseBean value) {
                                    if (value.getCode() == CODE_SUCC) {
                                        Toast.makeText(TTShopDetailActivity.this, "您已成功收藏"+post_title, Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(TTShopDetailActivity.this, "您已收藏过"+post_title, Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                public void onError(Throwable e) {
                                    super.onError(e);
                                    Toast.makeText(TTShopDetailActivity.this, R.string.network_err, Toast.LENGTH_LONG).show();
                                }
                            });
                }
                break;
            case R.id.showMomenyBtn:
                Intent intent = new Intent( Intent.ACTION_DIAL, Uri.parse( "tel:" + mobileString ) );
                intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
                startActivity( intent );
//                showCmmtPop( view );
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
